//---------------------------------------------------------------------
//
//---------------------------------------------------------------------

class ConstSTO extends STO {
	// ----------------------------------------------------------------
	// Constants have a value, so you should store them here.
	// Note: We suggest using Java's Double class, which can hold
	// floats and ints. You can then do .floatValue() or
	// .intValue() to get the corresponding value based on the
	// type. Booleans/Ptrs can easily be handled by ints.
	// Feel free to change this if you don't like it!
	// ----------------------------------------------------------------
	private Double m_value;

	// ----------------------------------------------------------------
	//
	// ----------------------------------------------------------------
	public ConstSTO(String strName) {
		super(strName);
		m_value = null; // fix this
		// You may want to change the isModifiable and isAddressable
		// fields as necessary
	}

	public ConstSTO(String strName, Type typ) {
		super(strName, typ);
		m_value = null;
	}

	public ConstSTO(String strName, Type typ, String value) {
		super(strName, typ);
		String newValue = DoHexOctalCheck(value);
		m_value = (double) Double.parseDouble(newValue);
	}
	
	public ConstSTO(String strName, Type typ, int value) {
		super(strName, typ);
		m_value = (double) value;
	}

	private String DoHexOctalCheck(String value) {
		String result = value;
		if(value.startsWith("0x") || value.startsWith("0X")) {
			// hex
			int res = Integer.parseInt(value.substring(2), 16);
			result = Integer.toString(res); 
		}
		else if(value.startsWith("0") && value.length() != 1) {
			if(!value.startsWith("0.")) {
				// octal
				Integer res = Integer.parseInt(value.substring(1), 8);
				result = res.toString();
			}
		}
		return result;
	}

	// ----------------------------------------------------------------
	// Check 2: Added extra ctor to take an extra parameter
	// isAddressable and set isModifiable to be false always.
	// (A ConstSTO is never modifiable).
	// ----------------------------------------------------------------
	public ConstSTO(String strName, Type typ, boolean isAddressable) {
		super(strName, typ);
		if(isAddressable) {
			setIsNonModLValue();
		} else {
			setIsRValue();
		}
	}

	// ----------------------------------------------------------------
	//
	// ----------------------------------------------------------------
	public boolean isConst() {
		return true;
	}

	// ----------------------------------------------------------------
	//
	// ----------------------------------------------------------------
	public void setValue(double val) {
		m_value = new Double(val);
	}

	// ----------------------------------------------------------------
	//
	// ----------------------------------------------------------------
	public Double getValue() {
		return m_value;
	}

	// ----------------------------------------------------------------
	//
	// ----------------------------------------------------------------
	public int getIntValue() {
		return m_value.intValue();
	}

	// ----------------------------------------------------------------
	//
	// ----------------------------------------------------------------
	public float getFloatValue() {
		return m_value.floatValue();
	}

	public boolean getBoolValue() {
		return m_value.intValue() != 0;
	}
}
